/**
 * @Title: index.js
 * @Description: index
 * @author: Fanchao
 * @date: 2020/12/13 8:12 下午
 * @version V1.0
 */
import React, { useState, useEffect } from "react";
import styles from "./index.less";
import SectionTitle from "components/SectionTitle";
import AddSomeButton from "components/AddSomeButton";
import { Button, Form, Input, message, Modal, Popconfirm, Table } from "antd";
import { createTrainingType, deleteTrainingType, getTrainingTypeList } from "api/index";

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 20 }
};
const TrainType = () => {
    const [loading, setLoading] = useState(false);
    const [dataSource, setDataSource] = useState([]);
    const [visible, setVisible] = useState(false);
    const [form] = Form.useForm();
    const fetch = async () => {
        setLoading(true);
        const result = await getTrainingTypeList();
        if (result.status === "success") {
            const dataSource = result.result;
            setDataSource(dataSource);
        } else {
            message.error(result.message);
        }
        setLoading(false);
    };
    useEffect(() => {
        fetch();
    }, []);
    const addItem = () => {
        setVisible(true);
    };
    const handleDelete = async ({ trainTypeId }) => {
        const result = await deleteTrainingType({ trainingTypeId: trainTypeId });
        if (result.status === "success") {
            message.success("删除成功");
            fetch();
        } else {
            message.error(result.message);
        }
    };
    const title = "新增培训类型";
    const columns = [
        {
            title: "培训类型名称",
            dataIndex: "trainingTypeName",
            align: "left"
        },
        {
            title: "操作",
            align: "left",

            render: (item) => {
                return (
                    <div>
                        <Popconfirm title="是否要删除？" onConfirm={() => handleDelete(item)}>
                            <Button style={{ padding: 2, color: "#C21217", fontWeight: "bold" }} type="link">删除</Button>
                        </Popconfirm>

                    </div>
                );
            }
        }
    ];
    return (
        <div className={styles.pageContainer}>
            <div className={styles.pageContainerHeader}>
                <SectionTitle title={title}/>
                <div style={{ width: "220px" }} onClick={() => addItem({})}>
                    <AddSomeButton title={title}/>
                </div>
            </div>
            <div className={styles.pageContainerTable}>
                <SectionTitle title={"已有培训类型"}/>
                <div className={styles.tableContainer}>
                    <Table
                        bordered={false} // 是否有边框
                        rowKey='trainTypeId' // 唯一key值
                        loading={loading} // 加载动画
                        columns={columns} // 表头
                        dataSource={dataSource}
                        pagination={false}
                    />
                </div>
            </div>
            <Modal
                title={title}
                visible={visible}
                // width="960px"
                onOk={() => {
                    form.validateFields().then(async values => {
                        const result = await createTrainingType(values);
                        if (result.status === "success") {
                            setVisible(false);
                            fetch();
                        } else {
                            message.error(result.message);
                        }
                    }).catch(info => {
                        // console.log(info);
                    });
                }}
                onCancel={() => {
                    form.resetFields();
                    setVisible(false);
                }}
            >
                <Form
                    {...layout}
                    layout="horizontal"
                    form={form}
                    name="basic"
                    // 表单默认值，只有初始化以及重置时生效 莫用
                    initialValues={{
                        // name: itemData.name
                    }}
                >
                    <Form.Item
                        label="培训类型"
                        name="trainingTypeName"
                        rules={[{ required: true, message: "必填" }]}
                    >
                        <Input placeholder="请输入培训类型名称"/>
                    </Form.Item>

                </Form>
            </Modal>
        </div>
    );
};

export default TrainType;
